from datetime import datetime
from app import db

class UploadedFile(db.Model):
    """上传文件模型"""
    __tablename__ = 'uploaded_files'
    
    id = db.Column(db.Integer, primary_key=True)
    original_filename = db.Column(db.String(255), nullable=False)
    oss_filename = db.Column(db.String(500), nullable=False, unique=True)
    oss_url = db.Column(db.String(500), nullable=False)
    file_size = db.Column(db.Integer, nullable=False)
    file_type = db.Column(db.String(50), nullable=False)
    
    # 关联的装修请求
    decoration_request_id = db.Column(db.Integer, db.ForeignKey('decoration_requests.id'), nullable=True)
    
    # 时间戳
    created_at = db.Column(db.DateTime, default=datetime.utcnow)
    
    def to_dict(self):
        """转换为字典格式"""
        return {
            'id': self.id,
            'original_filename': self.original_filename,
            'oss_filename': self.oss_filename,
            'oss_url': self.oss_url,
            'file_size': self.file_size,
            'file_type': self.file_type,
            'decoration_request_id': self.decoration_request_id,
            'created_at': self.created_at.isoformat() if self.created_at else None
        }
    
    def __repr__(self):
        return f'<UploadedFile {self.id}: {self.original_filename}>'
